package com.pswv2.admin.user.action;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.admin.user.service.AdminUserService;
import com.pswv2.bub.bsc.dao.po.AdminUser;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.Sessions;

@Controller
@RequestMapping(value = "adminLoginAction" ,produces = { "text/json;charset=UTF-8" })
public class AdminLoginAction {
	@Autowired
	private AdminUserService adminUserService;
	/**
	 * pc
	 * @param account
	 * @param password
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "platformPcAdminLogin")
	public String platformPcAdminLogin(String account, String password, HttpSession session) {
		AdminUser user = adminUserService.queryByAccount(account);
		if (null == user) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_not_exist).toJsonString();
		}
		if (!user.getPassword().equals(password)) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_password_not_match).toJsonString();
		}
		Sessions.setLoginedUser(user, session);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 退出账号
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "platformLogout")
	public String platformLogout(HttpSession session) {
		Sessions.removeLoginedUser(session);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
